package com.ruoyi.api.customer;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.CrmDataStatus;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CrmJob;
import com.ruoyi.system.domain.CrmUser;
import com.ruoyi.system.domain.CrmUserRecord;
import com.ruoyi.system.service.ICrmUserRecordService;
import com.ruoyi.system.service.ICrmUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2020-10-14
 */
@RestController
@RequestMapping("/api/crmUserRecord")
public class CrmUserRecordController extends BaseController
{
    @Autowired
    private ICrmUserRecordService crmUserRecordService;
    @Autowired
    private ICrmUserService crmCustomerServiceImpl;
    /**
     * 根据客户id获取记录列表信息
     * @return
     */
    @GetMapping("/getListByCusId")
    public AjaxResult getListByCusId(Long customerId)
    {
        if(customerId == null){
            return AjaxResult.error("用户无效");
        }
        CrmUserRecord CrmUserRecord=new CrmUserRecord();
        CrmUserRecord.setCusId(customerId);
        List<CrmUserRecord> list = crmUserRecordService.selectCrmUserRecordList(CrmUserRecord);
        return AjaxResult.success(list);
    }
    /**
     * 根据客户id获取记录列表信息
     * @return
     */
    @GetMapping("/getList")
    public AjaxResult getList(
            @RequestParam(value="logContent",required=false) String  logContent
    )
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId=loginUser.getUser().getUserId();
        CrmUserRecord CrmUserRecord=new CrmUserRecord();
        CrmUserRecord.setCreateUser(userId);
        CrmUserRecord.setLogContent(logContent);
        startPage();
        List<CrmUserRecord> list = crmUserRecordService.selectCrmUserRecordList(CrmUserRecord);
        List<Long> labelListLong = list.stream().map(s -> s.getCusId()).collect(Collectors.toList());
        List<CrmUser> crmCustomers = crmCustomerServiceImpl.selectCrmCustomerListByIds(labelListLong);
        for(CrmUserRecord crmUserRecord:list){
            for(CrmUser crmUser:crmCustomers){
                if(crmUserRecord.getCusId()!=null && crmUser.getId() !=null && crmUserRecord.getCusId().longValue()==crmUser.getId().longValue()){
                    crmUserRecord.setCrmUser(crmUser);
                    break;
                }
            }

        }
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 获取记录详细信息
     */
    @GetMapping(value = "/getRecordDetail")
    public AjaxResult getInfo(Long id)
    {
        if(id == null){
            return AjaxResult.error("记录id 无效");
        }
        return AjaxResult.success(crmUserRecordService.deleteCrmUserRecordById(id));
    }


    /**
     * 添加事件记录
     * @param crmLog
     * @return
     */
    @PostMapping("/insertCrmUserRecord")
    public AjaxResult insertCrmLog(@RequestBody         CrmUserRecord crmLog){
        if(StringUtils.isEmpty(crmLog.getLogContent())){
            return AjaxResult.error("添加失败,无记录内容");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId=loginUser.getUser().getUserId();
        crmLog.setCreateUser(userId);
        crmLog.setCreateTime(new Date());
        crmLog.setStatus(CrmDataStatus.OK.getCode());
        return crmUserRecordService.insertCrmUserRecord(crmLog)> 0 ? AjaxResult.success("添加成功"): AjaxResult.error("添加失败");
    }

    /**
     * 删除事件记录
     */
    @PostMapping("/deleteCrmUserRecordById")
    public AjaxResult remove(Long id)
    {
        if(id == null){
            return AjaxResult.error("事件记录id无效");
        }
        return toAjax(crmUserRecordService.deleteCrmUserRecordById(id));
    }
}
